local skel = fk.CreateSkill {
  name = "rmt__mouding",
  dynamic_desc = function (self, player, lang)
    local trick, skill = player:getMark("rmt__xingbian_trickcount"), player:getMark("rmt__mouding_skillcount")
    local x = math.abs(trick - skill)
    return "rmt__mouding_dyn:" .. x .. ":" .. trick .. ":" .. skill
  end,
}

Fk:loadTranslationTable{
  ["rmt__mouding"] = "谋定",
  [":rmt__mouding"] = "你可以将一张锦囊牌当弃牌堆的第X张普通锦囊牌使用，你可以发动武将牌堆第X张武将牌上的一个无标签技能"..
  "（X为你使用锦囊牌与发动其他技能的次数之差）。",

  [":rmt__mouding_dyn"] = "你可以将一张锦囊牌当弃牌堆的第X张普通锦囊牌使用，你可以发动武将牌堆第X张武将牌上的一个无标签技能（X={1}，为你使用锦囊牌({2})与发动其他技能的次数({3})之差）。",

  ["#rmt__mouding"] = "谋定：你可以将一张锦囊牌当【%arg】使用",

  ["$rmt__mouding1"] = "社稷之重，可倾山河湖海，负肩当驱千里。",
  ["$rmt__mouding2"] = "乱臣如逆潮当空，臣宁舍残躯以遏浊流。",
}

--- 获取视为使用的牌名
---@param player Player
local function getViewAsName(player)
  local n = math.abs(player:getMark("rmt__xingbian_trickcount") - player:getMark("rmt__mouding_skillcount"))
  local cards = table.filter(Fk:currentRoom().discard_pile, function (id) -- 客户端能查弃牌堆咯
    return Fk:getCardById(id):isCommonTrick()
  end)
  return (n > 0 and n <= #cards) and Fk:getCardById(cards[n]).name or ""
end

skel:addEffect("viewas", {
  pattern = ".|.|.|.|.|trick",
  prompt = function (self, player, selected_cards, selected)
    return "#rmt__mouding:::"..getViewAsName(player)
  end,
  card_num = 1,
  card_filter = function (self, player, to_select, selected)
    return #selected == 0 and Fk:getCardById(to_select).type == Card.TypeTrick
  end,
  view_as = function (self, player, cards)
    if #cards ~= 1 then return end
    local card = Fk:cloneCard(getViewAsName(player))
    card:addSubcard(cards[1])
    card.skillName = self.name
    return card
  end,
  enabled_at_play = function(self, player)
    return getViewAsName(player) ~= ""
  end,
  enabled_at_response = function(self, player, response)
    if not response and Fk.currentResponsePattern then
      local name = getViewAsName(player)
      return name ~= "" and Exppattern:Parse(Fk.currentResponsePattern):matchExp(".|.|.|.|"..name)
    end
  end,
})

--- 更新视为拥有的技能
---@param player ServerPlayer
local function MoudingClear(player, card_only)
  local room = player.room
  local n = math.abs(player:getMark("rmt__xingbian_trickcount") - player:getMark("rmt__mouding_skillcount"))
  if true then
    local skills, skills_orig = {}, table.simpleClone(player:getTableMark("rmt__mouding_skills"))
    if n > 0 and n <= #room.general_pile then
      local general = room.general_pile[n]
      for _, s in ipairs(Fk.generals[general]:getSkillNameList()) do
        local skill = Fk.skills[s]
        if skill:getSkeleton() and #skill:getSkeleton().tags == 0 then
          table.insert(skills, s)
        end
      end
    end
    room:setPlayerMark(player, "rmt__mouding_skills", skills)
    local skills_changed = table.filter(skills, function (s) return not player:hasSkill(s, true) end)
    for _, s in ipairs(skills_orig) do
      if not table.contains(skills, s) then
        table.insert(skills_changed, "-" .. s)
      end
    end
    if #skills_changed > 0 then -- 检测到变化再更新，防止频繁更新
      room:handleAddLoseSkills(player, skills_changed, nil, false, false)
    end
  end
end

skel:addEffect(fk.AfterCardUseDeclared, {
  can_refresh = function (self, event, target, player, data)
    if player:hasSkill(skel.name, true) then
      return target == player and data.card.type == Card.TypeTrick
    end
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:addPlayerMark(player, "rmt__xingbian_trickcount", 1)
    MoudingClear(player)
  end,
})

skel:addEffect(fk.AfterSkillEffect, {
  can_refresh = function (self, event, target, player, data)
    if player:hasSkill(skel.name, true) then
      local skill = data.skill
      return target == player and skill and skill:isPlayerSkill(player) and not skill.is_delay_effect
      and player:hasSkill(data.skill, true) and skill:getSkeleton() and skill:getSkeleton().name ~= skel.name
    end
  end,
  on_refresh = function (self, event, target, player, data)
    player.room:addPlayerMark(player, "rmt__mouding_skillcount", 1)
    MoudingClear(player)
  end,
})


return skel
